package com.wmm.service;

import com.wmm.bean.Classes;
import com.wmm.bean.PageBean;
import com.wmm.bean.Student;
import com.wmm.dao.ClassDao;
import com.wmm.factory.DaoFactory;

import java.util.List;

public class ClassesService {

    private ClassDao classDao;

    public ClassesService() {
        this.classDao = DaoFactory.getClassDao();
    }

    /**
     * 自定义成员方法实现根据参数指定的Classes对象来调用DAO层实现学生增加功能
     * @param classes
     * @return
     */
    public boolean addClassesServer(Classes classes){ return classDao.addClasses(classes);}

    /**
     * 自定义成员方法实现根据参数指定的Classes对象来调用DAO层实现学生删除功能
     * @param classNo
     * @return
     */
    public boolean deleteClassesServer(String classNo,String classNumber){
        if ("0".equals(classNumber)){
            return classDao.deleteClasses(classNo);
        }else {
            return false;
        }
    }

    /**
     * 自定义成员方法实现根据参数指定的Classes对象来调用DAO层实现学生更新功能
     * @param classes
     * @return
     */
    public boolean updateClassesServer(Classes classes){ return classDao.updateClasses(classes);}

    /**
     * 自定义成员方法实现根据参数指定的Classes对象来调用DAO层实现查询学生功能
     * @return
     */
    public List<Classes> queryClassesServer(String classNo, String className){ return classDao.queryClasses(classNo,className);}

    /**
     * 自定义成员方法实现班级人数增加
     * @param classNo
     * @return
     */
    public boolean addNumberServer(String classNo){ return classDao.addNumber(classNo);}

    /**
     * 自定义成员方法实现班级人数减少
     * @param classNo
     * @return
     */
    public boolean reduceNumberServer(String classNo){return classDao.reducceNumber(classNo);}

    /**
     * 分页查询，发现全部的学生数据
     * @param _currentPage
     * @param _rows
     * @return
     */
    public PageBean<Classes> findClassesByPage(String _currentPage, String _rows) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //1. 创建空的PageBean对象
        PageBean<Classes> pb = new PageBean<Classes>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3. 调用dao查询总记录数
        int totalCount = classDao.findTotalCount();
        pb.setTotalCount(totalCount);
        //4. 调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<Classes> list = classDao.findByPage(start,rows);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows == 0) ? totalCount / rows : totalCount / rows + 1;
        pb.setTotalPage(totalPage);
        return pb;

    }


    /**
     * 分页查询，发现查询的学生数据
     * @param _currentPage
     * @param _rows
     * @return
     */
    public PageBean<Classes> findQueryClassesByPage(String _currentPage, String _rows,String bystudentno, String byname) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //1. 创建空的PageBean对象
        PageBean<Classes> pb = new PageBean<Classes>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3. 调用dao查询总记录数
        int totalCount = classDao.findqueryTotalCount(bystudentno, byname);
        pb.setTotalCount(totalCount);
        //4. 调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<Classes> list = classDao.findqueryByPage(start,rows,bystudentno,byname);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows == 0) ? totalCount / rows : totalCount / rows + 1;
        pb.setTotalPage(totalPage);
        return pb;

    }

}
